import { handleActions } from 'redux-actions'

const init = {
	profile: {},
	coreProfile: {},
	reportLineInfo: {},
	// 提醒设置
	remindSettings: [],
  // 用户权限
  roles: []
}

export default handleActions({
  'fetch role': (state, { payload }) => ({
    ...state,
    roles: payload
  }),
	'fetch remind settings': (state, { payload }) => ({
		...state,
		remindSettings: payload
	}),
	'reset user': (state, action) => ({
		...state,
		...init
	}),
	'fetch user profile': (state, { payload }) => ({
		...state,
		profile: payload
	}),
  'save user profile': (state, { payload }) => ({
    ...state,
    profile: payload,
  }),
  // 临时变动user employment
  'change user employment': (state, { payload }) => {
    return {
      ...state,
      profile: {
        ...state.profile,
        userEmploymentVO: payload
      }
    }
  },
	'clear user profile': (state, { payload }) => ({
		...state,
		profile: {}
	}),
	'fetch user core profile': (state, { payload }) => ({
		...state,
		coreProfile: payload
	}),
	'clear user core profile': (state, { payload }) => ({
		...state,
		coreProfile: {}
	}),
	'fetch team': (state, { payload }) => ({
		...state,
		profile: {
			...state.profile,
			teamName: payload.teamName
		}
	}),
	'fetch reportLine info': (state, action) => ({
		...state,
		reportLineInfo: action.payload
	}),
  /*
	'save user profile': (state, { payload }) => {
		// update fields
		const keys = Object.keys(payload)
		return {
			...state,
			profile: {
				...state.profile,
				profileFieldVOs: (state.profile.profileFieldVOs || []).map(item => {
					if (~keys.indexOf(item.referenceName)) {
						return {
							...item,
							dataValue: payload[item.referenceName]
						}
					}
					return item
				})
			}
		}
	},
  */
  'update user reducer profile': (state, { payload }) => ({
    ...state,
    profile: {
      ...state.profile,
      ...payload
    }
  })
}, init)
